package com.hwbsite.hwbback.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwbsite.hwbback.consstants.ResConstant;
import com.hwbsite.hwbback.entity.WbReplyDTO;
import com.hwbsite.hwbback.entity.WeiboPublishDTO;
import com.hwbsite.hwbback.entity.group.IdGroup;
import com.hwbsite.hwbback.entity.group.ProductGroup;
import com.hwbsite.hwbback.service.WeiboService;
import com.hwbsite.hwbback.utils.DhRes;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/tt/wb")
@AllArgsConstructor
@Slf4j
public class WeiboController {

    private final WeiboService weiboService;

    @PostMapping("/sendWB")
    public DhRes sendWB(@Validated(ProductGroup.class) WeiboPublishDTO dto){
        try {
            return weiboService.saveWB(dto);
        }catch (Exception e){
            log.error("发布微博异常" ,e);
            return DhRes.fail(ResConstant.SYSTEM_ERROR_CODE);
        }

    }

    @PostMapping("/saveWBFile")
    public DhRes saveWBFile(@Validated(IdGroup.class) WeiboPublishDTO dto){
        try {
            return weiboService.saveWBFile(dto);
        }catch (Exception e){
            log.error("发布微博文件异常" ,e);
            return DhRes.fail(ResConstant.SYSTEM_ERROR_CODE);
        }

    }

    @PostMapping("/delete")
    public DhRes deleteWB(@RequestParam String id){
        try {
            return weiboService.deleteWB(id);
        }catch (Exception e){
            log.error("删除微博异常" ,e);
            return DhRes.fail(ResConstant.SYSTEM_ERROR_CODE);
        }

    }

    @GetMapping("/getWb")
//    @Cacheable
    public DhRes getWb(Page page, WeiboPublishDTO dto){
        return DhRes.success(weiboService.getWb(page , dto));
    }

    @GetMapping("/getWbC")
//    @Cacheable
    public DhRes getWbC( WeiboPublishDTO dto){
        return weiboService.getWbC(dto);
    }

    @GetMapping("/goodJob")
    public DhRes goodJob(@RequestParam("wbId") String wbId){
        return weiboService.goodJob(wbId);
    }

    @PostMapping("/replay")
    public DhRes comment(@RequestBody WbReplyDTO wbReplyDTO){
        return weiboService.comment(wbReplyDTO);
    }
}
